﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Timers;
using System.Windows.Threading;
namespace Twi.COMMON.WPF
{
    public class Tools
    {
        /// <summary>
        /// 判断是否是整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInt(string value)
        {
            int outValue;
            if (int.TryParse(value, out outValue))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 通过时间和随机数生成ID
        /// </summary>
        /// <returns></returns>
        public static string GenerateIDByTimeAndRandom()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(999).ToString();
        }


        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(str);
        }

        /// <summary>
        /// 判断是否Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDecimal(string value)
        {
            decimal outValue;
            if (decimal.TryParse(value, out outValue))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 判断是否浮点型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsFloat(string value)
        {
            float outValue;
            if (float.TryParse(value, out outValue))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 判断是否双浮点型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDouble(string value)
        {
            double outValue;
            if (double.TryParse(value, out outValue) && !outValue.Equals(Double.NaN))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="obj">错误信息</param>
        public static void ShowErrorInfo(object obj)
        {
            ErrorInfo er = new ErrorInfo(obj);
            er.Show();
        }

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="obj">错误信息</param>
        public static void ShowErrorInfo(string title, object obj)
        {
            ErrorInfo er = new ErrorInfo(title, obj);
            er.Show();
        }

        /// <summary>
        /// 输入备注
        /// </summary>
        /// <param name="reamark"></param>
        /// <returns></returns>
        public static bool ShowInputRemark(ref string reamark, string title = "备注：", bool isNeed = true)
        {
            bool isContinue = false;
            InputRemark ir = new InputRemark(isNeed);
            ir.tebTitle.Text = title;
            ir.TxtReamark.Text = reamark;
            ir.Topmost = true;
            ir.Activate(); //不加这句焦点还是无法自动进入文本框
            ir.TxtReamark.Focus();
            ir.ShowDialog();
            if (ir.DialogResult.Value)
            {
                reamark = ir.TxtReamark.Text;
                isContinue = true;
            }
            return isContinue;
        }

        /// <summary>
        /// 确认对话框信息提示
        /// </summary>
        /// <param name="gMI"></param>
        /// <returns></returns>
        public static bool GAffirmOKCancel(GMessageInfo gMI)
        {
            if (MessageBox.Show(gMI + "？", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
                return false;
            return true;
        }


        /// <summary>
        /// 确认对话框信息提示
        /// </summary>
        /// <param name="gMI"></param>
        /// <returns></returns>
        public static bool GAffirmOKCancel(string message)
        {
            if (MessageBox.Show(message + "？", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.Cancel) == MessageBoxResult.Cancel)
                return false;
            return true;
        }

        /// <summary>
        /// 确认对话框信息提示
        /// </summary>
        /// <param name="gMI"></param>
        /// <returns></returns>
        public static bool GAffirmYesNo(GMessageInfo gMI)
        {
            if (MessageBox.Show(gMI + "？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
                return false;
            return true;
        }

        public static bool GAffirmYesNo(string message)
        {
            if (MessageBox.Show(message + "？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
                return false;
            return true;
        }

        /// <summary>
        /// 信息提示
        /// </summary>
        /// <param name="message"></param>
        public static void GMessageInfo(GMessageInfo gmessage)
        {
            GMessageInfo(gmessage.ToString());
        }

        /// <summary>
        /// 信息提示
        /// </summary>
        /// <param name="message"></param>
        private static ToolTipWin tooltip;
        public static void GMessageInfo(string msg)
        {
            TwiMsgWin twiMsgWin = new TwiMsgWin();
            twiMsgWin.tbMsg.Text = msg;
            twiMsgWin.gridLeft.Background = new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString("#31708f"));
            twiMsgWin.gridMsg.Background = new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString("#d9edf7"));
            twiMsgWin.Show();
        }
        //static System.Timers.Timer aTimer = new System.Timers.Timer();
        ///// <summary>
        ///// Timer的Elapsed事件处理程序
        ///// </summary>
        ///// <param name="source"></param>
        ///// <param name="e"></param>
        //private static void OnTimedEvent(object source, ElapsedEventArgs e)
        //{
        //    aTimer.Elapsed -= new ElapsedEventHandler(OnTimedEvent);
        //    aTimer.Stop();
        //    tooltip.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
        //    {
        //        tooltip.Hide();
        //    });
        //}
        #region 窗体最大化（无边框）
        public static void WindowMax(Window win)
        {
            win.WindowState = System.Windows.WindowState.Normal;
            win.WindowStyle = System.Windows.WindowStyle.None;
            win.ResizeMode = System.Windows.ResizeMode.NoResize;
            //win.Topmost = true;
            win.Left = 0.0;
            win.Top = 0.0;
            win.Width = SystemParameters.WorkArea.Width;
            win.Height = SystemParameters.WorkArea.Height;
        }
        #endregion
        #region 获得简析颜色值
        /// <summary>
        /// 获得简析颜色值
        /// </summary>
        /// <param name="dataColor">(241,15,155)</param>
        /// <returns></returns>
        public static SolidColorBrush GetColor(string dataColor)
        {
            SolidColorBrush sColorBrush = Brushes.Black;
            if (!string.IsNullOrEmpty(dataColor))
            {
                string[] colorRGB = dataColor.Split(',');
                byte r = 0;
                byte g = 0;
                byte b = 0;
                if (colorRGB.Length == 3)
                {
                    byte.TryParse(colorRGB[0], out r);
                    byte.TryParse(colorRGB[1], out g);
                    byte.TryParse(colorRGB[2], out b);
                }
                sColorBrush = new SolidColorBrush(Color.FromRgb(r, g, b));
            }
            return sColorBrush;
        }
        #endregion
    }

    public enum GMessageInfo
    {
        确定要撤销吗 = 1,
        保存成功 = 2,
        保存失败 = 3,
        提交成功 = 4,
        提交失败 = 5,
        确定要删除吗 = 6,
        请至少选择一项 = 7,
        请选中一项 = 8,
        密码已重置 = 9,
        折扣不能大于100 = 13,
        确定要清空吗 = 15,
        密码已修改 = 52,
        确定要导出全部记录 = 54,
        删除成功 = 57,
        确定要退回吗 = 58,
        无权删除 = 59,
        无权修改 = 60,
        确定要取消吗 = 106,
        确定要重置吗 = 108,
        确定要升级吗 = 110,
        没有可修改项 = 115,
        日期输入有误 = 118,
        周期输入有误 = 120,
        信息已发送 = 122
    }

    public enum FileType
    {
        其他 = 0,
        Word = 1,
        Excel = 2,
        PDF = 3
    }
}
